package org.chen.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

@Configuration
public class MovieAsyncConfig {

    @Value("${task.execution.pool.core-size:10}")
    private int corePoolSize;

    @Value("${task.execution.pool.max-size:20}")
    private int maxPoolSize;

    @Value("${task.execution.pool.queue-capacity:100}")
    private int queueCapacity;

    @Value("${task.execution.pool.keep-alive:60}")
    private int keepAlive;

    /**
     * 电影处理异步任务线程池
     * 用于处理电影批量上传等异步任务
     */
    @Bean("movieTaskExecutor")
    public AsyncTaskExecutor movieTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(corePoolSize);  // 核心线程数
        executor.setMaxPoolSize(maxPoolSize);   // 最大线程数
        executor.setQueueCapacity(queueCapacity); // 增加队列容量
        executor.setKeepAliveSeconds(keepAlive);
        executor.setThreadNamePrefix("movie-async-");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 添加拒绝策略
        executor.initialize();
        return executor;
    }
}